<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page import="servlets.message.*"%>
<%@ page import="org.hibernate.SessionFactory"%>
<%@ page import="org.hibernate.Session"%>
<%@ page import="java.util.List"%>
<%@ page import="hibernate.model.*"%>
<%@ page import="hibernate.util.HibernateUtil"%>
<%@ page import="hibernate.method.*"%>
<%@ page import="hibernate.method.UserMethods"%>
<meta charset="UTF-8">

<script> 
$('select').change(function(e){
	 e.preventDefault();
	 var clickedID = $(this).val();
	  $.ajax({
	       url: "../deleteGame",
	       type: "get",//type of posting the data
	       data: {
		       game_id: clickedID
	       },
	       dataType: 'json',
	       success: function (json) {
			var data = json;
			$("#gm").hide().fadeIn(1000).html("<table>");
		    $("#gm").append("<tr><td>Nazwa:</td><td>"+data.game_name+"</td></tr>");
		    $("#gm").append("<tr><td>Data dodania:</td><td>"+data.add_date+"</td></tr>");
		    $("#gm").append("<tr><td>Minimalna ilosc graczy:</td><td>"+data.min_users+"</td></tr>");
		    $("#gm").append("<tr><td>Maksymalna ilosc graczy:</td><td>"+data.max_users+"</td></tr>");
		    $("#gm").append("<tr><td>Opis:</td><td>"+data.description+"</td></tr>");
		    $("#gm").append("</table>");
	       },
	       error: function(xhr, ajaxOptions, thrownError){
	       },
	       timeout : 15000
	  });

	});

</script>




<div class="page-header">
	<h2 class="page-title">Usuwanie gry</h2>
</div>

<br />

<div id="content">

<%
						SessionFactory sessionFactory = HibernateUtil
								.getSessionAnnotationFactory();
						Session s = sessionFactory.openSession();
						s.beginTransaction();
						GameMethods gm = new GameMethods();
							response.setContentType("text/html");
						
						List<Game> gamelist = gm.listAllGames(sessionFactory, s);
						
						s.getTransaction().commit();
					%> 
			<form action="../deleteGame" method="POST" name='formularz'>		
<table>
<tr>
<td>
			Gra:
			</td><td>
			<select id="game" name="game">
						<option value="0" selected>Wybierz:</option>
						<%	if (gamelist.size() > 0) {
							for (Game g : gamelist) {
								String gamename = g.getName();
								int id = g.getGame_id();%>
						<option value="<%=id%>"><%=gamename%></option>
						<%}
							
						}
						if (gamelist.size() == 0) {
					 out.println("Nie ma zadnych gier w bazie");
					
						} %>
						</select>
		
		</td>
		</tr>
				<tr>
				<td><input type="submit" class="mainBtn" value="Usun gre" /></td>
			</tr>

</table>
</form>

</div>
<br/><br />
<div id="gameDetails">
<div class="page-header">
<h2 class="page-title">Dane gry</h2>
</div>
<div id="gm"></div>
</div>